package fun.coding.leetcode;

public class CompareVersionNumbers {

	public static void main(String[] args) {
		CompareVersionNumbers ins = new CompareVersionNumbers();
		System.out.println(ins.compareVersion("1", "1.1"));
		// Good catch!
		System.out.println(ins.compareVersion("1.0", "1"));
	}
	
	public int compareVersion(String version1, String version2) {
		String[] v1 = version1.split("\\."); // . means any char in regex, so escape it 
		String[] v2 = version2.split("\\.");
		
		int length = Math.max(v1.length, v2.length);
		
		int i = 0;
		
		while (i < length) {
			int t1 = i >= v1.length ? 0 : Integer.parseInt(v1[i]);
			int t2 = i >= v2.length ? 0 : Integer.parseInt(v2[i]);
			if (t1 > t2) {
				return 1;
			} else if (t1 < t2) {
				return -1;
			} else {
				i++;
			}
		}
		
		return 0;
	}

}
